package com.zx.auth.block.controller;

import com.zx.auth.block.param.OAuth2ClientParam;
import com.zx.auth.block.service.IOAuth2ClientService;
import com.zx.auth.block.vo.OAuth2RegisteredClientVO;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.core.wrapper.PageWrapper;
import com.zx.core.wrapper.ResponseWrapper;
import com.zx.log.annotation.OperateLog;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import com.zx.validation.groups.Create;
import com.zx.validation.groups.Query;
import com.zx.validation.groups.Update;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * OAuth2客户端管理控制器
 * 提供OAuth2客户端的增删改查接口
 */
@RestController
@RequestMapping("/oauth2/client")
@RequiredArgsConstructor
public class OAuth2ClientController {

    private final IOAuth2ClientService oAuth2ClientService;

    /**
     * 创建新的OAuth2客户端
     *
     * @param clientParam 客户端参数
     * @return 创建结果
     */
    @PreAuthorize("hasAuthority('oauth2:client:add')")
    @OperateLog(value = "新增客户端")
    @PostMapping("/addClient")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "客户端参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Create.class})
    })
    public ResponseWrapper createClient(@RequestBody OAuth2ClientParam clientParam) {
        return oAuth2ClientService.createClient(clientParam);
    }

    /**
     * 根据ID获取OAuth2客户端信息
     *
     * @param id 客户端ID
     * @return 客户端信息
     */
    @PreAuthorize("hasAuthority('oauth2:client:info')")
    @OperateLog(value = "获取客户端信息")
    @GetMapping("/info/{id}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "客户端ID", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ObjectWrapper<OAuth2RegisteredClientVO> getClientById(@PathVariable String id) {
        return oAuth2ClientService.getClientById(id);
    }

    /**
     * 分页查询OAuth2客户端列表
     *
     * @param clientParam 客户端参数
     * @return 客户端列表
     */
    @PreAuthorize("hasAuthority('oauth2:client:list')")
    @OperateLog(value = "分页查询客户端")
    @PostMapping("/list")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "客户端参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Query.class})
    )
    public PageWrapper<OAuth2RegisteredClientVO> listClients(@RequestBody OAuth2ClientParam clientParam) {
        return oAuth2ClientService.listClients(clientParam);
    }

    /**
     * 更新OAuth2客户端信息
     *
     * @param clientParam 更新的客户端参数
     * @return 更新后的客户端信息
     */
    @PreAuthorize("hasAuthority('oauth2:client:edit')")
    @OperateLog(value = "编辑客户端")
    @PutMapping("/editClient")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "客户端参数", rules = {ValidateRulesEnum.OBJECT}, groups = {Update.class})
    })
    public ResponseWrapper updateClient(@RequestBody OAuth2ClientParam clientParam) {
        return oAuth2ClientService.updateClient(clientParam);
    }

    /**
     * 删除OAuth2客户端
     *
     * @param id 客户端ID
     * @return 删除结果
     */
    @PreAuthorize("hasAuthority('oauth2:client:remove')")
    @OperateLog(value = "删除客户端")
    @DeleteMapping("/deleteClient/{id}")
    @ParameterValidations({
            @ParameterValidation(index = 0, desc = "客户端ID", rules = {ValidateRulesEnum.NOT_NULL})
    })
    public ResponseWrapper deleteClient(@PathVariable String id) {
        return oAuth2ClientService.deleteClient(id);
    }
}